/* Copyright 2008 Bas van den Berg
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <string>

#include "yaffut.h"
#include "myassert.h"
#include "TestRoom.h"

#include "color.h"
#include "TaskQueue.h"
#include "Area.h"
#include "config.h"

#define private public
#include "MudExit.h"
#include "TrainTask.h"
#undef private

using namespace std;
using namespace Chronos;


class TrainTaskTest {
public:
    TrainTaskTest()
        : taskQueue(0)
		, area(0, "")
        , train(new TestRoom("Inside train"))
        , station1(new TestRoom("station1"))
        , station2(new TestRoom("station2"))
        , station3(new TestRoom("station3"))
        , task(new TrainTask(area, 1, new MudRoomExit("train", "", "", "", "", area, 1)))
    {
        area.addRoom(1, *train);
        area.addRoom(2, *station1);
        area.addRoom(3, *station2);
        area.addRoom(4, *station3);
        task->addDestRoom(2, new MudRoomExit("out", "", "", "", "", area, 2));
        task->addDestRoom(3, new MudRoomExit("out", "", "", "", "", area, 3));
        task->addDestRoom(4, new MudRoomExit("out", "", "", "", "", area, 4));
        taskQueue.addTask(task, 1);
    }
    void waitTime(unsigned int delay) {
        for (unsigned int i=0; i<delay; ++i) taskQueue.runNextTime();
    }

    TaskQueue taskQueue;
    Area area;
    TestRoom* train;
    TestRoom* station1;
    TestRoom* station2;
    TestRoom* station3;
    TrainTask* task;
};


TEST(TrainTaskTest, testGetNextDest) {
    ASSERT_EQUAL(2, task->getNextDest().roomID);
    ASSERT_EQUAL(3, task->getNextDest().roomID);
    ASSERT_EQUAL(4, task->getNextDest().roomID);
    ASSERT_EQUAL(2, task->getNextDest().roomID);
}


TEST(TrainTaskTest, testTrain) {
	ASSERT_NULL(train->findObject("out"));
	ASSERT_NULL(train->findObject("train"));
    waitTime(1);
    train->ROOM_CHECKWRITTEN("The train arrives at the station and the doors open.\n");
    station1->ROOM_CHECKWRITTEN("The train arrives at the station and the doors open.\n");
    ASSERT_NOT_NULL(train->findObject("out"));
    ASSERT_NOT_NULL(station1->findObject("train"));

    waitTime(seconds(10)-1);
    ASSERT_NOT_NULL(train->findObject("out"));
    ASSERT_NOT_NULL(station1->findObject("train"));

    waitTime(1);
    train->ROOM_CHECKWRITTEN("The doors close and the train leaves the station.\n");
    station1->ROOM_CHECKWRITTEN("The doors close and the train leaves the station.\n");
    station2->ROOM_CHECKWRITTEN("");
    station3->ROOM_CHECKWRITTEN("");
	ASSERT_NULL(train->findObject("out"));
	ASSERT_NULL(train->findObject("train"));
}

